<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="__STATIC__/js/jquery.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css">
    <script type="text/javascript" src="__STATIC__/x-admin/js/xadmin.js"></script>
    <link rel="stylesheet" href="__STATIC__/x-admin/css/font.css">
    <link rel="stylesheet" href="__STATIC__/x-admin/css/xadmin.css">
</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="{:url('/admin/index')}"></a>
        <a href=""></a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div style="height: 30px"></div>
<div style="width: 500px;font-size: 10px">
    <form class="layui-form" action="" onsubmit="return false" id = 'adminAdd'>
        <div class="layui-form-item">
            <label class="layui-form-label">管理员姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" id="username"  placeholder="请输入管理员姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">管理员账号</label>
            <div class="layui-input-block">
                <input type="text" name="login" id="account"  placeholder="请输入管理员账号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">管理员密码</label>
            <div class="layui-input-block">
                <input type="password" name="pwd" id="pwd"  placeholder="密码由6-20个字母、数字、下划线组成" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" id="password" placeholder="确认密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">权限分配</label>
            <div class="layui-input-block">
                {foreach $rule as $value}
                    <input type="radio" name="rule" value="{$value.id}" title="{$value.scope_name}">
                {/foreach}
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="insert">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
    //表单初始化
    layui.use('form', function(){
        var form = layui.form;
        layer = layui.layer;
    });
</script>
<script>
    $("#insert").click(function(){
        //密码正则  只能输入6-20个字母、数字、下划线
        var patrn = /^(\w){6,20}$/;
        var username = $("#username").val();
        var account = $("#account").val();
        var pwd = $("#pwd").val();
        var password = $("#password").val();
        if(!username){
            layer.msg('请输入管理员姓名',{icon:5,time:2000});
            return;
        }
        if(!account){
            layer.msg('账号不能为空',{icon:5,time:2000});
            return;
        }
        if(!patrn.test(pwd)){
            layer.msg('密码格式不正确',{icon:5,time:2000});
            return;
        }
        if(pwd != password){
            layer.msg('两次密码不一致',{icon:5,time:2000});
            return;
        }
        if(!$("input[name=rule]:checked").val()){
            layer.msg('请选择管理员权限',{icon:5,time:2000});
            return;
        }
        $.ajax({
            url:"{:url('/admin/insert')}",
            data:$("#adminAdd").serialize(),
            type:"post",
            dataType:"json",
            success:function(res){
                if(res.code){
                    layer.msg(res.msg,{icon:1,time:2000},function(){
                        window.location.reload();
                    });
                    return;
                }else{
                    layer.msg(res.msg,{icon:5,time:2000});
                    return;
                }
            }
        });
    });
</script>
</body>
</html>